home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / D-G / Generic init / generic init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-23  |  8.0 KB  |  306 lines  |  [TEXT/KAHL]

  1.  
  2. /************************************************************
  3.  
  4. generic timed init.c
  5.  
  6.  
  7.             Copyright © 1993, Gregory Babior
  8.             All Rights Reserved
  9.  
  10. BE WARNED - used as is this init will put up an alert every
  11. ten seconds until you hold down the command key while dismissing
  12. the alert.
  13.  
  14. This is a generic timed init that makes use of the Time
  15. Manager and the Notification Manager.  It uses both in this
  16. way:
  17.  
  18. 1) It queues up a delayed call to the Time Manager.
  19.  
  20. 2) Because the Time Manager is called a interrupt time and
  21. we want to perform a task which involves moving memory (not 
  22. allowed at interrupt time) the only thing our call does is
  23. to queue up a call to the Notification Manager, which will 
  24. execute during GetNextEvent/WaitNextEvent time, allowing us
  25. to do what we will.
  26.  
  27. 3) When we have completed our task (through using the response
  28. routine allowed in a Notification record), we queue up another
  29. call to the Time Manager which, when executed, will queue up
  30. the same Notification record, which at its end calls the Time
  31. Manager - etc.etc.etc.
  32.  
  33. BE WARNED - used as is this init will put up an alert every
  34. ten seconds until you hold down the command key while dismissing
  35. the alert.
  36.  
  37.  
  38. This was my first init and I'm trying to save folks the hell
  39. I went through in figuring it out.  I'm sure there are ways
  40. to optimize the code and if anyone can make improvements,
  41. in the code or in the commented documentation, please do so 
  42. and pass them on.  Lord knows there is a dearth of info about
  43. the structure and execution of inits.
  44.  
  45. ************************************************************/
  46.  
  47. #include "SetUpA4.h"                    
  48. #include "GestaltEqu.h"
  49. #include "Aliases.h"
  50. #include "Timer.h"
  51. #include "Folders.h"
  52. #include "Notification.h"
  53.  
  54. struct NMRec        theNoteRec;
  55.  
  56. typedef struct             /* Time Manager information record */
  57. {    
  58.     TMTask    atmTask;    /* original and revised TMtask record */
  59.     long    tmRefCon;    /* space to pass address of A4 world */
  60. } TMInfo;
  61.  
  62. typedef TMInfo *TMInfoPtr;
  63.  
  64. pascal TMInfoPtr GetTMInfo (void)        /* recover address of Time Manager information record */
  65.     = 0x2E89;                            /* MOVE.L A1,(SP) */
  66.  
  67. TMInfo    myTMInfo;    /* a TM information record */
  68.  
  69.  
  70. pascal void DoTheJob(theNoteRecPtr)
  71. NMRecPtr    theNoteRecPtr;
  72. {
  73.     OSErr        theErr;
  74.     short        foundVRefNum;
  75.     long        foundDirID;
  76.     FSSpec        newSpec;
  77.     short        initRefNum;
  78.     long        myA4;
  79.     short        oldResFile;
  80.     EventRecord    theEvent;
  81.     oldResFile = CurResFile();    /* store current resource file id */
  82.     
  83.      asm                /* Push all registers because I'm either paranoid or careful */
  84.     {
  85.         move.l d0, -(sp)
  86.         move.l d1, -(sp)
  87.         move.l d2, -(sp)
  88.         move.l d3, -(sp)
  89.         move.l d4, -(sp)
  90.         move.l d5, -(sp)
  91.         move.l d6, -(sp)
  92.         move.l d7, -(sp)
  93.         
  94.         move.l a0, -(sp)
  95.         move.l a1, -(sp)
  96.         move.l a2, -(sp)
  97.         move.l a3, -(sp)
  98.         move.l a4, -(sp)
  99.         move.l a5, -(sp)
  100.         move.l a6, -(sp)
  101.         move.l a7, -(sp)
  102.     }                    
  103.     
  104.     myA4 = theNoteRecPtr->nmRefCon;    /* recall ptr to A4 address which was stored a boot time */
  105.     asm 
  106.     {
  107.         move.l    myA4, a0;        /* Get ptr into A0     */
  108.     }
  109.     RememberA0();
  110.     SetUpA4();                
  111.     
  112.     /**********************************************************************************
  113.         here we are going to locate the file that contains the resources we want to use
  114.         (in this case it is the init file itself), access the file, do the work, and
  115.         restore the reference number for the original resource (saved above) as the 
  116.         current resource.  NOTE - the code uses the name of the file to find it so 
  117.         changing the name will cause problems.
  118.     ***********************************************************************************/
  119.     
  120.     theErr = FindFolder(kOnSystemDisk,'extn',kDontCreateFolder,&foundVRefNum,&foundDirID);
  121.     theErr = FSMakeFSSpec(foundVRefNum,foundDirID,"\pgeneric init",&newSpec);
  122.     
  123.     if(theErr == 0)
  124.     {
  125.         initRefNum = FSpOpenResFile(&newSpec,fsRdWrShPerm);
  126.         
  127.         if (initRefNum != -1)    /* if we have a valid resource file */
  128.         {
  129.             UseResFile(initRefNum);
  130.             Alert(128,nil);            /* do the job now (could do whatever) */
  131.             UseResFile(oldResFile);    /* restore original resource file id */
  132.             CloseResFile(initRefNum);
  133.         }
  134.     }
  135.     theErr = NMRemove(theNoteRecPtr);
  136.     
  137.     GetNextEvent(everyEvent,&theEvent);
  138.     
  139.     /*********************************************************************************** 
  140.         if command key is down, do nothing.  This breaks the calling loop.  The code 
  141.         will do nothing from here on out. If it's not down, activate the next Time
  142.         Manager call.
  143.     ***********************************************************************************/
  144.     
  145.     if(theEvent.modifiers & cmdKey)                    
  146.         ;
  147.     else
  148.         PrimeTime( (QElemPtr) &myTMInfo, (10000) );    /* activate the next record */
  149.                                                     /* delay of 10 sec             */
  150.                                                     
  151.     RestoreA4();                                    /* restore original A4 */
  152.     
  153.     asm                        /* Pop all stored data and restore environment */
  154.     {
  155.         move.l (sp)+, a7 
  156.         move.l (sp)+, a6 
  157.         move.l (sp)+, a5 
  158.         move.l (sp)+, a4 
  159.         move.l (sp)+, a3 
  160.         move.l (sp)+, a2 
  161.         move.l (sp)+, a1 
  162.         move.l (sp)+, a0 
  163.         
  164.         move.l (sp)+, d7 
  165.         move.l (sp)+, d6 
  166.         move.l (sp)+, d5 
  167.         move.l (sp)+, d4 
  168.         move.l (sp)+, d3 
  169.         move.l (sp)+, d2 
  170.         move.l (sp)+, d1 
  171.         move.l (sp)+, d0 
  172.     }                    
  173. }
  174.  
  175.  
  176.  
  177. pascal void TheTask()    /* Yikes - look out - it's interrupt time */
  178. {
  179.     
  180.     OSErr        theErr;
  181.     TMInfoPtr    recPtr;
  182.     long        myA4;
  183.     
  184.     
  185.      asm                /* Push all registers because I'm either paranoid or careful */
  186.     {
  187.         move.l d0, -(sp)
  188.         move.l d1, -(sp)
  189.         move.l d2, -(sp)
  190.         move.l d3, -(sp)
  191.         move.l d4, -(sp)
  192.         move.l d5, -(sp)
  193.         move.l d6, -(sp)
  194.         move.l d7, -(sp)
  195.         
  196.         move.l a0, -(sp)
  197.         move.l a1, -(sp)
  198.         move.l a2, -(sp)
  199.         move.l a3, -(sp)
  200.         move.l a4, -(sp)
  201.         move.l a5, -(sp)
  202.         move.l a6, -(sp)
  203.         move.l a7, -(sp)
  204.     }                    
  205.     
  206.     
  207.     recPtr = GetTMInfo();        /* get the address to the Time Manager record */
  208.     
  209.     myA4 = recPtr->tmRefCon;    /* recall ptr to A4 address which was stored a boot time */
  210.     asm 
  211.     {
  212.         move.l    myA4, a0;        /* Get ptr to A0     */
  213.     }
  214.     RememberA0();
  215.     SetUpA4();                
  216.     
  217.     theErr = NMInstall(&theNoteRec);
  218.     
  219.     
  220.     RestoreA4();                        /* restore original A4 */
  221.     
  222.     asm                        /* Pop all stored data and restore environment */
  223.     {
  224.         move.l (sp)+, a7 
  225.         move.l (sp)+, a6 
  226.         move.l (sp)+, a5 
  227.         move.l (sp)+, a4 
  228.         move.l (sp)+, a3 
  229.         move.l (sp)+, a2 
  230.         move.l (sp)+, a1 
  231.         move.l (sp)+, a0 
  232.         
  233.         move.l (sp)+, d7 
  234.         move.l (sp)+, d6 
  235.         move.l (sp)+, d5 
  236.         move.l (sp)+, d4 
  237.         move.l (sp)+, d3 
  238.         move.l (sp)+, d2 
  239.         move.l (sp)+, d1 
  240.         move.l (sp)+, d0 
  241.     }                    
  242. }
  243.     
  244.     
  245.  
  246. InstallTMTask(initHndl)
  247. Handle    initHndl;
  248. {
  249.     myTMInfo.atmTask.tmAddr     = TheTask;            /* get address of task     */
  250.     myTMInfo.atmTask.tmWakeUp     = 0;                /* initialize tmWakeUp */
  251.     myTMInfo.atmTask.tmReserved = 0;                /* initialize tmReserved */
  252.     myTMInfo.tmRefCon             = (long)*initHndl;    /* store address of A4 */
  253.     
  254.     InsTime((QElemPtr) &myTMInfo);                    /* install the 1st record */
  255.     PrimeTime((QElemPtr) &myTMInfo, (10000));        /* activate the 1st record     */
  256. }                                                    /* delay of 10 sec             */
  257.  
  258. main()
  259. {
  260.     Ptr                initPtr;
  261.     Handle            initHndl;
  262.     long            theResponse;
  263.     OSErr            theErr;
  264.     
  265.     asm 
  266.     {
  267.         move.l    a0,initPtr;        /* Get the pointer to this code     */
  268.     }
  269.     RememberA0();
  270.     SetUpA4();
  271.     
  272.     initHndl = RecoverHandle(initPtr);
  273.     HLock(initHndl);
  274.     DetachResource(initHndl);    /* leave code in system heap */
  275.     HNoPurge(initHndl);            /* make sure it stays there */
  276.     
  277.     
  278.     theErr = Gestalt('sysv',&theResponse);    
  279.     if (theResponse < 0x0700)                /* if less than system 7 do nothing  */
  280.     {
  281.         ;
  282.     }
  283.     else
  284.     {
  285.         /***********************************************************************
  286.             Set up the notification record. This is being used so that we can 
  287.             call a routine (DoTheJob) and have actions occur that are not allowed 
  288.             at interrupt time.
  289.         ************************************************************************/ 
  290.     
  291.         theNoteRec.qType     = 8;
  292.         theNoteRec.nmMark     = 0;
  293.         theNoteRec.nmIcon     = nil;                    /* We want no fireworks            */
  294.         theNoteRec.nmSound     = nil;                    /* from the Notification Manager, */
  295.         theNoteRec.nmStr     = nil;                    /* we just want the routine       */
  296.         theNoteRec.nmResp     = (NMProcPtr)DoTheJob;    /* "DoTheJob" to execute.           */
  297.         theNoteRec.nmRefCon = (long)*initHndl;        /* Store pointer to boot time A4  */
  298.         
  299.         InstallTMTask(initHndl);         /* go install first task */
  300.     }
  301.     
  302.     RestoreA4();
  303. }
  304.  
  305.  
  306.